package com.hundredapp.oa.repository;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hundredapp.oa.dao.GenericDao;
import com.hundredapp.oa.dao.SelectType;
import com.hundredapp.oa.domain.OaClass;
import com.hundredapp.oa.domain.OaContract;
import com.hundredapp.oa.domain.OaElect;
import com.hundredapp.oa.domain.OaSign;
import com.hundredapp.oa.domain.OaStudent;
import com.hundredapp.oa.domain.OaSubject;
import com.hundredapp.oa.vo.GraduationVO;

@Repository
public class ClassmngManagerImpl implements ClassmngManager{

	@Autowired
	private GenericDao<OaClass, Integer> classDao;
	
	@Autowired
	private GenericDao<OaStudent, Integer> studentDao;
	
	@Autowired
	private GenericDao<OaSign,Integer> signDao;
	
	@Autowired
	private GenericDao<OaContract,Integer> contractDao;
	
	@Autowired
	private GenericDao<OaElect,Integer> electDao;
	
	@Override
	public int add(OaClass classset) {
		return classDao.save(classset);
	}
	
	@Override
	public List<OaClass> findAll(String condition) {
		return classDao.find(condition);
	}
	
	@Override
	public List<OaClass> findAll(String condition,int start,int  limit) {
		return classDao.find(condition,start,limit);
	}
	
	@Override
	public void delete(int id){
		classDao.remove(classDao.findById(id));
	}
	
	@Override
	public List<OaStudent> findStudent(int id){
		return classDao.findById(id).getOaStudents();
	}
	
	@Override
	public List<OaStudent> findUnselectStudent(int id){
		List<OaStudent> allstudent=new ArrayList<OaStudent>();
		OaSubject sub=classDao.findById(id).getOaSubject();
		for(OaElect elect:electDao.find(" oaSubject.id = "+sub.getId())){
			OaContract con=elect.getOaContract();
			for(OaStudent stu:con.getOaStudents()){
				switch(classDao.findById(id).getAge()){
				case 1:
					if(stu.getAge()>3 && stu.getAge()<7){
						allstudent.add(stu);
					}
					break;
				case 2:
					if(stu.getAge()>6 && stu.getAge()<10){
						allstudent.add(stu);
					}
					break;
				case 3:
					if(stu.getAge()>9 && stu.getAge()<13){
						allstudent.add(stu);
					}
					break;
				}
			}
		}
		List<OaStudent> selected=classDao.findById(id).getOaStudents();
		allstudent.removeAll(selected);
		return allstudent;
	}
	
	@Override
	public OaClass findById(int id){
		return classDao.findById(id);
	}
	
	@Override
	public void update(OaClass oaclazz){
		classDao.modify(oaclazz);
	}
	
	@Override
	public List<GraduationVO> findGraduation(int classid, int worktime){
		List<GraduationVO> result=new ArrayList<GraduationVO>();
		for(OaStudent student:classDao.findById(classid).getOaStudents()){
			GraduationVO temp=new GraduationVO();
			temp.setStudentName(student.getDisplayName());
			for(OaSign sign:signDao.find("classId="+classid+"and studentId="+student.getId()+" and worktime="+worktime)){
				switch (sign.getWeek()) {
				case 1:
					temp.setW1(sign.getState());
					break;
				case 2:
					temp.setW2(sign.getState());
					break;
				case 3:
					temp.setW3(sign.getState());
					break;
				case 4:
					temp.setW4(sign.getState());
					break;
				case 5:
					temp.setW5(sign.getState());
					break;
				case 6:
					temp.setW6(sign.getState());
					break;
				case 7:
					temp.setW7(sign.getState());
					break;
				case 8:
					temp.setW8(sign.getState());
					break;
				case 9:
					temp.setW9(sign.getState());
					break;
				case 10:
					temp.setW10(sign.getState());
					break;
				case 11:
					temp.setW11(sign.getState());
					break;
				case 12:
					temp.setW12(sign.getState());
					break;
				}
			}
			result.add(temp);
		}
		return result;
	}

	public void setClassDao(GenericDao<OaClass, Integer> classDao) {
		this.classDao = classDao;
	}

	public void setStudentDao(GenericDao<OaStudent, Integer> studentDao) {
		this.studentDao = studentDao;
	}

	public void setSignDao(GenericDao<OaSign, Integer> signDao) {
		this.signDao = signDao;
	}

	@Override
	public OaClass findBySub(int id) {
		return classDao.findUnique("oaSubject.id="+id);
	}

}
